package viettravel.guiandevent;


import org.holoeverywhere.app.ProgressDialog;

import viettravel.DTO.DBCommunication;
import viettravel.common.Constants;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;

public class ViettravelSplashActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_viettravel_splash);
		new SyncData().execute();		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.viettravel_splash, menu);
		return true;
	}
	
	public class SyncData extends AsyncTask<Void, Void, String>{
		
		private ProgressDialog mProgressDialog;

		@Override
		protected void onPreExecute() {
			mProgressDialog = new ProgressDialog(ViettravelSplashActivity.this);
			mProgressDialog.setMessage(getString(R.string.loadingdata));
			mProgressDialog.setCancelable(false);
			mProgressDialog.setIndeterminate(true);
			mProgressDialog.show();
		};


		@Override
		protected String doInBackground(Void... params) {
			String data="";
			try{
			Log.v(Constants.TAG,"Come 1");
			
			data=DBCommunication.parserRegions(Constants.ROOT, Constants.SUFFIX_GET_REGIONS);
			Log.v(Constants.TAG,"Come 2" );
			data=DBCommunication.parserProvinces(Constants.ROOT, Constants.SUFFIX_GET_RPOVINCES);
			Log.v(Constants.TAG,"Come 3" );
			data=DBCommunication.parserPlaces(Constants.ROOT, Constants.SUFFIX_GET_PLACES);
			Log.v(Constants.TAG,"Come 4" );
			data=DBCommunication.parserSpecialties(Constants.ROOT, Constants.SUFFIX_GET_SPECIALTIESS);
			Log.v(Constants.TAG,"Come 5" );
			data=DBCommunication.parserTouristForms(Constants.ROOT, Constants.SUFFIX_GET_TOURISTFORMS);
			Log.v(Constants.TAG,"Come 6" );
			data=DBCommunication.parserActivities(Constants.ROOT, Constants.SUFFIX_GET_ACTIVITIES);
			}catch(Exception e){
				Log.v(Constants.TAG,"Loi parse data");
			}
			return data;
		}
		
		@Override
		protected void onPostExecute(String result)
		{
			if (mProgressDialog.isShowing()) 
			{
				mProgressDialog.dismiss();
				Intent intent=new Intent(getBaseContext(),ViettravelMainActivity.class);
				startActivity(intent);
				finish();
			}
		}
	}

}
